package com.demo.common;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.demo.common.controller.BlogController;
import com.demo.common.controller.IndexController;
import com.demo.common.model._MappingKit;
import com.demo.handler.HtmlHandler;
import com.demo.interceptor.GobalIterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

/**
 * ClassName: CommonConfig <br/>
 * Function: 实现校验器，文件上传. 把beetl为视图模板，加了beetl、antlr4两个jar。加了妹子：）注意路径。<br/>
 * Reason: 演示校验器的构成. <br/>
 * Date: 2017年1月5日 下午7:06:46 <br/>
 *
 * @author michael  郑鸿
 * @version 
 * @since JDK 1.8
 */
public class CommonConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值ֵ
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		/*loadPropertyFile("config.properties");
		me.setDevMode(true); */
		//me.setViewType(ViewType.JSP);
		
		//修改视图根目录，html不能放在WEB-INF/view中，因权限访问不到，但jsp可以
		//beetl.properties中不做设置，因为会与me.setBaseViewPath("WEB");设置冲突
		me.setBaseViewPath("WEB");
		
		me.setError404View("404.html");
		me.setBaseDownloadPath("/download");
		me.setBaseUploadPath(PathKit.getWebRootPath()+"/upload");
		me.setUrlParaSeparator("-");

		// beetl模版配置工厂
		BeetlRenderFactory beetlRenderFactory = new BeetlRenderFactory();
		me.setMainRenderFactory(beetlRenderFactory);
		// 获取GroupTemplate ,可以设置共享变量等操作
		GroupTemplate groupTemplate=BeetlRenderFactory.groupTemplate;
		//src 下面加个beetl.properties文件就可以用了

	}

	@Override
	public void configRoute(Routes me) {
		me.add("/blog", BlogController.class); 
		me.add("/front", IndexController.class);

	}
	
	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin C3p0Plugin = createC3p0Plugin();
		//C3p0Plugin C3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password"));
		me.add(C3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
		me.add(arp);
		//arp.addMapping("blog", Blog.class);
		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new GobalIterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new HtmlHandler());
	}
	
	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		System.out.println("JFinal Start! Staring my thread.");
	}
	
	@Override
	public void beforeJFinalStop() {
		super.beforeJFinalStop();
		System.out.println("JFinal Stop! Stoping my thread."); 
	}
	
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
